package src.NPCs;

import java.io.*;
import src.Main;

public class LoadNPCLists
{
    public LoadNPCLists()
    {
        loadNPCList("NPC.CFG");
        loadNPCs("NPCSPAWN.CFG");
    }

    private boolean loadNPCs(String file)
    {
        String line = "", token = "", token2 = "", token2_2 = "", token3[] = new String[10];
        BufferedReader cfgFile = null;
        try
        {
            cfgFile = new BufferedReader(new FileReader("./src/Data/CFG/" + file));
            line = cfgFile.readLine().trim();
        }
        catch(Exception e)
        {
            Main.m.log("Error loading NPCs.");
            line = token = token2 = token2_2 = null;
            token3 = null;
            return false;
        }
        while(line != null)
        {
            int index = line.indexOf("=");
            if(index > -1)
            {
                token = line.substring(0, index).trim();
                token2 = line.substring(index + 1).trim();
                token2_2 = token2.replaceAll("\t\t", "\t");
                token3 = token2_2.split("\t");
                if(token.equals("Spawn"))
                {
                    Main.m.ge.newNPC(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3]), 
                    Integer.parseInt(token3[4]), Integer.parseInt(token3[5]), Integer.parseInt(token3[6]), Integer.parseInt(token3[7]), true);
                }
            }
            else if(line.equals("[ENDOFSPAWNLIST]"))
            {
                try
                {
                    cfgFile.close();
                }
                catch(IOException ioe)
                {}
                cfgFile = null;
                line = token = token2 = token2_2 = null;
                token3 = null;
                return true;
            }
            try
            {
                line = cfgFile.readLine();
            }
            catch(IOException ioe)
            {
                line = null;
                line = token = token2 = token2_2 = null;
                token3 = null;
                cfgFile = null;
                return false;
            }
        }
        try
        {
            cfgFile.close();
            cfgFile = null;
        }
        catch(IOException ioe)
        {
        }
        return false;
    }

    private boolean loadNPCList(String fileName)
    {
        String line = "", token = "", token2 = "", token2_2 = "", token3[] = new String[10];
        BufferedReader list = null;
        try
        {
            list = new BufferedReader(new FileReader("./src/Data/CFG/" + fileName));
            line = list.readLine().trim();
        }
        catch(Exception e)
        {
            Main.m.log("Error loading NPC Lists.");
            return false;
        }
        while(line != null)
        {
            int spot = line.indexOf("=");
            if(spot > -1)
            {
                token = line.substring(0, spot).trim();
                token2 = line.substring(spot + 1).trim();
                token2_2 = token2.replaceAll("\t\t", "\t");
                token2_2 = token2_2.replaceAll("\t\t", "\t");
                token2_2 = token2_2.replaceAll("\t\t", "\t");
                token3 = token2_2.split("\t");
                if(token.equals("NPC"))
                {
                    newNPCList(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), 
                    Integer.parseInt(token3[3]), Integer.parseInt(token3[4]), Integer.parseInt(token3[5]), 
                    Integer.parseInt(token3[6]), token3[7].replaceAll("_", " "));
                }
            }
            else
            {
                if(line.equals("[ENDOFNPCLIST]"))
                {
                    try
                    {
                        list.close();
                    }
                    catch(IOException ioexception)
                    {}
                    list = null;
                    return true;
                }
            }
            try
            {
                line = list.readLine().trim();
            }
            catch(IOException ioexception1)
            {
                try
                {
                    list.close();
                }
                catch(IOException ioexception)
                {}
                list = null;
                return true;
            }
        }
        return false;
    }

    private void newNPCList(int npcType, int cbLevel, int maxHP, int maxHit, int atkType, int weakness, int spawnTime, String name)
    {
        if(npcType >= Main.m.ge.maxListedNPCs)
        {
            Main.m.log("maxListedNPCs size is to low");
            return;
        }
        Main.m.ge.npcLists[npcType] = new NPCList(npcType, name, cbLevel, maxHP, maxHit, atkType, weakness, spawnTime);
    }

}